package com.guofen.service.shlj.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.pojo.ShljAbnormalGarbage;
import com.guofen.pojo.ShljDeodorizationtowerWaterChange1;
import com.guofen.util.DateAndTimeUtil;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.guofen.service.shlj.EquipmentMaintenanceService;
import com.guofen.dao.ShljEquipmentMaintenanceMapper;
import com.guofen.pojo.ShljEquipmentMaintenance;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class EquipmentMaintenanceServiceImpl implements EquipmentMaintenanceService{
    @Autowired
    private ShljEquipmentMaintenanceMapper shljEquipmentMaintenanceMapper;

    @Override
    public boolean add(String equipmentName,
                       String operator,
                       String method,
                       String examiner,
                       String res,
                       String remarks){
        Date now = new Date();
        String date = DateAndTimeUtil.getDate();
        SimpleDateFormat dayFormat = new SimpleDateFormat("MM/dd");
        String day = dayFormat.format(now);
        SimpleDateFormat weekFm = new SimpleDateFormat("EEEE", Locale.CHINA);
        String week = weekFm.format(now);

        ShljEquipmentMaintenance shljEquipmentMaintenance = new ShljEquipmentMaintenance();
        shljEquipmentMaintenance.setDate(date);
        shljEquipmentMaintenance.setEquipmentName(equipmentName);
        shljEquipmentMaintenance.setMaintenanceContent("");//废弃之后删
        shljEquipmentMaintenance.setPlanAintenanceWeek("周日");
        shljEquipmentMaintenance.setActualAintenanceWeek(week);
        shljEquipmentMaintenance.setActualAintenanceDay(day);
        shljEquipmentMaintenance.setOperator(operator);
        shljEquipmentMaintenance.setMethod(method);
        shljEquipmentMaintenance.setExaminer(examiner);
        shljEquipmentMaintenance.setResult(res);
        shljEquipmentMaintenance.setRemarks(remarks);

        int result = shljEquipmentMaintenanceMapper.insertSelective(shljEquipmentMaintenance);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public boolean update(Integer id,
                       String equipmentName,
                       String operator,
                       String method,
                       String examiner,
                       String res,
                       String remarks){
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = dateFormat.format(now);
        SimpleDateFormat dayFormat = new SimpleDateFormat("MM/dd");
        String day = dayFormat.format(now);

        SimpleDateFormat weekFm = new SimpleDateFormat("EEEE", Locale.CHINA);
        String week = weekFm.format(now);


        ShljEquipmentMaintenance shljEquipmentMaintenance = new ShljEquipmentMaintenance();
        shljEquipmentMaintenance.setId(id);
        //shljEquipmentMaintenance.setDate(date);
        shljEquipmentMaintenance.setEquipmentName(equipmentName);
        shljEquipmentMaintenance.setMaintenanceContent("");//废弃之后删
        //shljEquipmentMaintenance.setPlanAintenanceWeek("周日");
        //shljEquipmentMaintenance.setActualAintenanceWeek(week);
        //shljEquipmentMaintenance.setActualAintenanceDay(day);
        shljEquipmentMaintenance.setOperator(operator);
        shljEquipmentMaintenance.setMethod(method);
        shljEquipmentMaintenance.setExaminer(examiner);
        shljEquipmentMaintenance.setResult(res);
        shljEquipmentMaintenance.setRemarks(remarks);

        int result = shljEquipmentMaintenanceMapper.updateByPrimaryKeySelective(shljEquipmentMaintenance);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public ShljEquipmentMaintenance get(Integer id){
        return shljEquipmentMaintenanceMapper.selectByPrimaryKey(id);
    }

    @Override
    public ShljEquipmentMaintenance getlatest(String equipmentName,String maintenanceContent){
        Map<String, Object> data = new HashMap<>();
        data.put("equipmentsName", equipmentName);
        data.put("maintenanceContent", maintenanceContent);
        return shljEquipmentMaintenanceMapper.selectLatest(data);
    }

    @Override
    public boolean is_latest_complete(String equipmentName,String menstanceNum){
        List<ShljEquipmentMaintenance> items = shljEquipmentMaintenanceMapper.selectlastByName(equipmentName);

        if (items.size() == Integer.parseInt(menstanceNum))
            return true;
        else
            return false;
    }

    @Override
    public List<Integer> is_lastweek_complete(String equipmentsName, String menstanceNum, String role){
        List<Integer> res = new ArrayList<Integer>();
        String[] equipments = equipmentsName.split("\\|");
        String[] num = menstanceNum.split("\\|");
        boolean breakkey = false;

        for(int i=0;i<equipments.length ;i++){
            breakkey = false;
            List<ShljEquipmentMaintenance> items = shljEquipmentMaintenanceMapper.selectlastByName(equipments[i]);
            if (items.size() == Integer.parseInt(num[i])){
                if(role.equals("examiner")){
                    for(int j=0;j<items.size();j++){
                        if(items.get(j).getExaminer()==null||items.get(j).getExaminer().equals("")){
                            res.add(1);
                            breakkey = true;
                            break;
                        }
                    }
                }
                if(breakkey)continue;
                res.add(2);
            }else{
                res.add(0);
            }
        }
        return res;
    }

    @Override
    public ShljEquipmentMaintenance getlast(){
        return shljEquipmentMaintenanceMapper.getLast();
    }

    @Override
    public int getInThisWeek() {
        return shljEquipmentMaintenanceMapper.selectInThisWeek();
    }

    @Override
    public List<ShljEquipmentMaintenance> getWeekRecords(String equipmentName) {
        return shljEquipmentMaintenanceMapper.getWeekRecords(equipmentName);
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<ShljEquipmentMaintenance> list = shljEquipmentMaintenanceMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<ShljEquipmentMaintenance> list = shljEquipmentMaintenanceMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return shljEquipmentMaintenanceMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(ShljEquipmentMaintenance record) {
        return shljEquipmentMaintenanceMapper.updateByPrimaryKeySelective(record);
    }
}